package vn.edu.saigontech.ncms.web.englishtest;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.englishtest.Candidate;
import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.service.CandidateService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateCandidateAction extends AbstractValidator {
	
	private Long id;
	private String firstName;
	private String lastName;
	private String gender;
	private String dateOfBirth;
	private String email;
	private String registeredDate;
	private String testDate;
	private Long programId;
	private Long placementShiftId;
	private Long consultantId;
	private String phoneNumber;
	
	public String check() {
		Candidate candidate = CandidateService.INSTANCE.findById(this.id); 
		if(!Checker.isNull(this.id)) {
			if(Checker.isNull(candidate)) {
				this.sendKeyError("validator.nonExist", "candidate");
			}
			if(candidate.getVersion() != this.version) {
				this.sendKeyError("validator.concurrency", "candidate");
			}
		}
		if(Checker.isEmpty(this.firstName)) {
			this.sendKeyError("validator.required", "firstName");
		}
		if(Checker.isEmpty(this.lastName)) {
			this.sendKeyError("validator.required", "lastName");
		}
		if(Checker.isEmpty(this.gender)) {
			this.sendKeyError("validator.required", "gender");
		}
		if(Checker.isEmpty(this.dateOfBirth)) {
			this.sendKeyError("validator.required", "dateOfBirth");
		}		
		if(!Checker.isDate(this.dateOfBirth, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "dateOfBirth", "format.date");
		}
		if(!Checker.isEmpty(this.email)) {
			if(!Checker.isEmail(this.email)) {
				this.sendKeyError("validator.invalid", "email");
			}
		}
		if(Checker.isEmpty(this.phoneNumber)) {
			this.sendKeyError("validator.required", "phoneNumber");
		}
		if(Checker.isNull(this.id)) {
			Candidate candidateSearch = new Candidate();
			candidateSearch.setFirstName(this.firstName);
			candidateSearch.setLastName(this.lastName);
			candidateSearch.setGender(Gender.create(this.gender));
			candidateSearch.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
			Collection<Candidate> candidates = CandidateService.INSTANCE.find(candidateSearch);
			if(!candidates.isEmpty())
				this.sendKeyError("validator.exist", "candidate");
		}
		if(!Checker.isEmpty(this.registeredDate)) {
			if(!Checker.isDate(this.registeredDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "consultedDate", "format.date");
			}
		}
		else {
			this.sendKeyError("validator.required", "consultedDate");
		}
		if(Checker.isNull(this.consultantId)) {
			this.sendKeyError("validator.required", "consultant");
		}
		return this.toSuccess();
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getRegisteredDate() {
		return registeredDate;
	}

	public void setRegisteredDate(String registeredDate) {
		this.registeredDate = registeredDate;
	}

	public String getTestDate() {
		return testDate;
	}

	public void setTestDate(String testDate) {
		this.testDate = testDate;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public Long getPlacementShiftId() {
		return placementShiftId;
	}

	public void setPlacementShiftId(Long placementShiftId) {
		this.placementShiftId = placementShiftId;
	}
}